package com.jd.finance.core.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jd.finance.core.pojo.entity.BankApply;
import com.jd.finance.core.service.BankApplyService;
import com.jd.finance.core.service.impl.AdminUtilsServiceImpl;
import com.jd.finance.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 银行申请表 前端控制器
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-25
 */
@Api(tags = "银行申请")
@Slf4j
@RestController
@RequestMapping("/admin/core/adminBankCenter")
public class AdminBankApplyController {

    @Resource
    private AdminUtilsServiceImpl adminUtilsServiceimpl;

    @Resource
    private BankApplyService bankApplyService;

    /**
     * 申请添加合作银行
     * @param uid
     * @param appLybName
     * @param appLybAgreement
     * @param appLyAddress
     * @param request
     * @return
     */
    @ApiOperation("申请添加合作银行")
    @PostMapping("/insertCoopBank")
    public BaseResult insertCoopBank(Integer uid, String appLybName, String appLybAgreement,
                                     String appLyAddress, HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid),appLybName,appLybAgreement,appLyAddress)) {
            log.error("申请信息为空!");
            return BaseResult.error().message("申请信息为空!");
        }

        if (!adminUtilsServiceimpl.isSale(uid)) {
            log.error("该用户非业务员身份!");
            return BaseResult.error().message("该用户非业务员身份!");
        }

        return bankApplyService.insertCoopBank(uid,appLybName,appLybAgreement,appLyAddress,request);
    }

    /**
     *修改 合作银行 审批状态 通过或不通过
     * @return
     */
    @ApiOperation("修改合作银行审核状态")
    @PostMapping("/updateExamApply")
    public BaseResult updateExamApply(Integer uid,Integer bankAppId,String appLyStatus,HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid),String.valueOf(bankAppId),appLyStatus)) {
            log.error("申请审批状态信息为空!");
            return BaseResult.error().message("申请审批状态信息为空!");
        }

        if (!adminUtilsServiceimpl.isGM(uid)) {
            log.error("该用户非总经理身份!");
            return BaseResult.error().message("该用户非总经理身份!");
        }

        return bankApplyService.updateExamApply(uid,bankAppId,appLyStatus,request);
    }

    /**
     * 查看申请状态(分页)
     * @return
     */
    @ApiOperation(("查看合作银行申请状态"))
    @GetMapping("/getExamStatus")
    public BaseResult getExamStatus(Long page, Long limit, Integer uid,HttpServletRequest request) {
        if(page==null || limit==null || uid==null) {
            log.error("查询申请状态提交信息为空!");
            return BaseResult.error().message("查询申请状态提交信息为空!");
        }

        if (!adminUtilsServiceimpl.isSale(uid)) {
            log.error("该用户非业务员身份!");
            return BaseResult.error().message("该用户非业务员身份!");
        }

        Page<BankApply> pageParam=new Page<>(page,limit);
        Page<BankApply> pageModel=bankApplyService.getExamStatus(pageParam,uid,request);

        List<BankApply> records=pageModel.getRecords();
        records.stream().filter(bean -> {
            String status=bean.getBankApplyStatus();
            if (status.equals("1")) {
                bean.setBankApplyStatus("通过");
            }else if (status.equals("2")) {
                bean.setBankApplyStatus("未通过");
            }else {
                log.error("信息错误!");
            }
            return true;
        }).collect(Collectors.toList());
        pageModel.setRecords(records);
        return BaseResult.ok().data("pageModel",pageModel);
    }


    @ApiOperation("根据申请id，查看相应申请状态")
    @GetMapping("/getBankApplyInfo")
    public BaseResult getBankApplyInfo(Integer uid,Integer appLyId) {
        if (uid==null || appLyId==null) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        if (!adminUtilsServiceimpl.isSale(uid)) {
            log.error("该用户非业务员身份!");
            return BaseResult.error().message("该用户非业务员身份!");
        }
        return bankApplyService.getBankApplyInfo(uid,appLyId);
    }


    @ApiOperation("添加合作银行")
    @PostMapping("/addCoopBank")
    public BaseResult addCoopBank(Integer uid,Integer appLyId,HttpServletRequest request) {
        if (uid==null || appLyId==null) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        if (!adminUtilsServiceimpl.isSale(uid)) {
            log.error("该用户非业务员身份!");
            return BaseResult.error().message("该用户非业务员身份!");
        }
        return bankApplyService.addCoopBank(uid,appLyId,request);
    }


}

